#include "socket.h"

// ./server server_port


string Execute_command(const string& command)
{
    cout<<"get a command :"<<command<<endl;
    FILE* fp = popen(command.c_str(),"r");//以读的方式打开文件，并且创建子进程与管道,程序替换执行命令，结果返回进文件中
    if(fp==nullptr)
    {
        return "call fails!!!";
    }

    string ret;
    char buffer[1024];
    while(fgets(buffer,sizeof(buffer),fp))
    {
        ret+=buffer;
    }
    pclose(fp);
    
    return ret.size()?"command not found!!!":ret;

}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        cerr << "erro input,please in:xxx port" << endl;
        exit(-1);
    }

    string ip;
    uint16_t port = stoi(argv[1]);

    unique_ptr<Udp> usv = make_unique<Udp>(ip, port,Execute_command);

    usv->Creat_socket();
    usv->Bind();
    usv->Run_server();

    return 0;
}